package com.java1234.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.java1234.dao.MessageDao;
import com.java1234.model.Message;
import com.java1234.model.PageBean;
import com.java1234.util.DbUtil;
import com.java1234.util.JsonUtil;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class MessageListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	DbUtil dbUtil=new DbUtil();
	MessageDao messageDao=new MessageDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String page=request.getParameter("page");
		String rows=request.getParameter("rows");
		//查询功能	
		String messageId=request.getParameter("messageId");
		String Name=request.getParameter("Name");
		String head=request.getParameter("head");
		String workspace=request.getParameter("workspace");
		String members=request.getParameter("members");
		String aid=request.getParameter("aid");
		
		if(messageId==""){
			messageId="0";
		}
		if(Name==null){
			Name="";
		}
		if(head==null){
			head="";
		}
		if(workspace==null){
			workspace="";
		}
		if(members==null){
			members="";
		}
		if(aid==null){
			aid="";
		}
	
		Message message=new Message();
		int id = 0;
		if(messageId != null){
			id=Integer.parseInt(messageId);
		}		
		
		message.setId(id);
		message.setName(Name);
		message.setHead(head);
		message.setWorkspace(workspace);
		message.setMembers(members);
		message.setAid(aid);
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		Connection con=null;
		JSONObject result=new JSONObject();
		int total = 0;
		try{
			con=dbUtil.getCon();
			
			JSONArray jsonArray=JsonUtil.formatRsToJsonArray(messageDao.messageList(con, pageBean, message));
			
			//搜索List数据
			List<Message> exportMessage= messageDao.exportSearchMessage(con,message);
			//int total = 0;
			if(StringUtil.isNotEmpty(message.getName())||StringUtil.isNotEmpty(message.getHead())||StringUtil.isNotEmpty(message.getWorkspace())||StringUtil.isNotEmpty(message.getMembers())||StringUtil.isNotEmpty(message.getAid())){
				total=messageDao.messageSearchCount(con,message);
			}else{
				total=messageDao.messageCount(con,message);
			}			
			result.put("rows", jsonArray);
			if(message.getId()!=0){
				total=1;
			}
			result.put("total", total);
			ResponseUtil.write(response, result);
		
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
